﻿using CaterBll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaterUI
{
    public partial class FormMain : Form
    {
        private OrderInfoBll oiBll = new OrderInfoBll();
        public FormMain()
        {
            InitializeComponent();
          
        }

        private void menuQuit_Click(object sender, EventArgs e)
        {
            //将当前应用程序退出，而不是当前窗体
            //this.Close();
            Application.Exit();
        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            //将当前应用程序退出，而不是当前窗体
            Application.Exit();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //判断登录用户级别，以确定是否显示menuManager图标
            int type = Convert.ToInt32(this.Tag);
            if(type==1)
            {
                //经理
            }
            else
            {
                menuManagerInfo.Visible = false;
            }
            //加载所有厅包信息
            LoadHallInfo();
        }

        private void menuManagerInfo_Click(object sender, EventArgs e)
        {
            FormManagerInfo formManagerInfo = FormManagerInfo.Create();
            formManagerInfo.Show();
            formManagerInfo.Focus();
        }

        private void LoadHallInfo()
        {
            //2.1获取所有厅包对象
            HallInfoBll hiBll = new HallInfoBll();
            var list = hiBll.GetList();
            //2.2遍历集合，向标签页中添加信息
            tabHallInfo.TabPages.Clear();
            TableInfoBll tiBll = new TableInfoBll();
            foreach (var hi in list)
            {
                //根据厅包对象创建标签页对象
                TabPage tp = new TabPage(hi.HTitle);
                //3.1、动态创建列表添加到标签页上
                ListView lvTableInfo = new ListView();
                //添加双击事件，完成开单功能
                lvTableInfo.DoubleClick += lvTableInfo_DoubleClick;
                //3.2 让列表使用图片
                lvTableInfo.LargeImageList = imageList1;
                lvTableInfo.Dock = DockStyle.Fill;
                tp.Controls.Add(lvTableInfo);
                //4.1 获取当前厅包对象的所有餐桌
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("thallId", hi.HId.ToString());
                var ListTableInfo = tiBll.GetList(dic);
                //4.2 向列表中添加餐桌信息
                foreach (var ti in ListTableInfo)
                {
                    var lvi = new ListViewItem(ti.TTitle, ti.TIsFree ? 0 : 1);
                    //后续用到编号，在这里存一下
                    lvi.Tag = ti.TId;
                    lvTableInfo.Items.Add(lvi);
                }
                //2.3 将标签加入到容器
                tabHallInfo.TabPages.Add(tp);
            }
        }

        void lvTableInfo_DoubleClick(object sender,EventArgs e)
        {
            //获取被点的餐桌项
            var lv1=sender as ListView;
            var lvi = lv1.SelectedItems[0];
            //获取餐桌编号
            int tableId = Convert.ToInt32(lvi.Tag);

            //图片索引为0，则是空闲
            if(lvi.ImageIndex==0)
            {
                //餐桌空闲，则可以开单
                //1 开单，想orderinfo表插入数据 2 修改餐桌状态为占用
                int orderId = oiBll.KaiOrder(tableId);
                lvi.Tag = orderId;

                //3更新图标为占用
                lv1.SelectedItems[0].ImageIndex = 1;
            }
            else
            {
                //餐桌已经占用，则进行点菜操作

            }
            //4 打开点菜窗体
            FormOrderDish formOrderDish = new FormOrderDish();
            formOrderDish.Tag = lvi.Tag;
            formOrderDish.Show();


        }

    }
}
